import store from "@/store"
import {loginByPhone} from "@/router.js"
//请求根地址
const base_url = "https://ldjk.ranyi.shop";

const ApiRequest = ({url, data = '', method = 'get', type = '', loading = true}) => { //prams 为我们需要调用的接口API的参数 下面会贴具体代码
    const header = {
        "content-type": type === 'form' ? 'application/x-www-form-urlencoded' : 'application/json',
        "Authorization": `Bearer ${store.state.user.token}`
    }
    return new Promise((resolve, reject) => {
        if (loading) uni.showLoading({title: '努力加载中~', mask: true})
        return uni.request({
            url: base_url + url,//请求的网络地址和局地的api地址组合
            header,
            data,
            timeout:0.5*1000,
            method: method.toUpperCase(),
            success: (res) => {
                if (res.data.code === 429) {
                    // debugger
                    console.log(loginByPhone)
                    uni.showToast({title: res.data.message, icon: "none"})
                    store.commit('user/remove_token')
                    uni.reLaunch({url: loginByPhone})
                    reject(res.data)
                    return;
                }
                if(res.data.code!==200){
                    uni.showToast({title: res.data.message, icon: "none"})
                }
                resolve(res.data);
                // console.log(res.data)
            },
            fail: (err) => {
                reject(err);
                console.log(err)
            },
            complete: (err) => {
                // console.log('请求完成')
                uni.hideLoading()
                if(err.errMsg)uni.showToast({icon: 'none',title: err.errMsg})
            }
        });
    })
}
export default ApiRequest

